﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CMS;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;
using System.IO;
using System.Drawing;

public partial class Register : System.Web.UI.Page
{
    public Functions fn = new Functions();

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!fn.LoginSucceed == true) { Response.Redirect("Login.aspx"); }
        if (!IsPostBack)
        {
            DataTable ADM = CMS.StoredProcedureValues.VW_Administration(null, null, null, true, null, null);
            dropAdministration.DataSource = ADM;
            dropAdministration.DataTextField = "ADM_NAME";
            dropAdministration.DataValueField = "ADM_ID";
            AdministrationAv.Visible = ADM.Rows.Count > 0;
            dropAdministration.DataBind();
        }

        MasterUserPanel.Visible = CMS.Functions.IsMasterUser() && CMS.Functions.UserID.HasValue;

        BtnCancel.Text = !CMS.Functions.VerifyLogin.Valid  ? "Back" : "Cancel";
    }

    public String hashPassword;
    public String RestrictedPassword;
    void UserPassword(Boolean ValidPassword)
    {
        if (ValidPassword)
        {
            hashPassword = CMS.Functions.Hash.ComputeHash(RestrictedPassword, "MD5", null);
        }
    }

    public Boolean Policy()
    {
        RestrictedPassword = CMS.Functions.PasswordPolicy(8, txtRegUserWachtwoord.Text, "The minimum length of a password must be atleast 8 characters and not " + txtRegUserWachtwoord.Text.Length + "", "$('.PasswordPolicy').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');", System.Drawing.KnownColor.Black);
        return !String.IsNullOrEmpty(RestrictedPassword);
    }

    protected void BtnRegister_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtRegUserName.Text) && !string.IsNullOrEmpty(txtRegUserWachtwoord.Text) && !string.IsNullOrEmpty(txtRegEmail.Text))
        {
            UserPassword(Policy());

            if (Policy())
            {
                if (CMS.Functions.GetEmailAddress(txtRegEmail.Text) != null)
                {

                    txtRegEmail.Text.ToLower();

                    StoredProcedureValues Results = new StoredProcedureValues();
                    Results = CMS.StoredProcedureValues.SP_User(CMD.Insert, null, Convert.ToInt64(dropAdministration.SelectedItem.Value), txtRegUserName.Text, hashPassword, txtRegEmail.Text, true, null, USR_MASTERUSER.Checked);
                        if (Results.Error != 2601) //2627
                        {
                            Session.Add("RegisteredUsername", txtRegUserName.Text);
                            Response.Redirect("Login.aspx");
                        }
                        else
                        {
                            ((Content)Master).Jscript = CMS.Functions.NormalPopup("Login already exsists with this email : " + txtRegEmail.Text + "<br /> please choose a different email !", System.Drawing.KnownColor.Black, "$('.Email').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');");
                        }
                }
                else
                {
                    ((Content)Master).Jscript = CMS.Functions.NormalPopup("Email is not valid !", System.Drawing.KnownColor.Black, "$('.ValidEmail').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');");
                }
            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.IsError;
            }

        }
        else
        {

            ((Content)Master).Jscript =
            String.IsNullOrEmpty(txtRegUserName.Text) ? CMS.Functions.NormalPopup("Please enter Username !",
            System.Drawing.KnownColor.Black,
            " $('.Username').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');") :
            String.IsNullOrEmpty(txtRegUserWachtwoord.Text) ? CMS.Functions.NormalPopup("Please enter Password !",
            System.Drawing.KnownColor.Black,
            " $('.Password').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');") :
            String.IsNullOrEmpty(txtRegEmail.Text) ? CMS.Functions.NormalPopup("Please enter Email !",
            System.Drawing.KnownColor.Black,
            " $('.Email').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');") : null;
        }
    }
   

    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        if (!CMS.Functions.VerifyLogin.Valid)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            Response.Redirect("Page.aspx");
        }
    }
    
}
    
              